import os

from PyPDF2 import PdfFileReader,PdfFileWriter


current_path = os.getcwd()
file_path = os.getcwd()+'/python_interview_exercise/jianshu/tool_codes/'
print(file_path)  # 获取当前工作目录路径)

output = PdfFileWriter()
with open(file_path+"s.pdf", 'rb') as pf:
    pin = PdfFileReader(pf)
    for i in range(pin.getNumPages()):
        page = pin.getPage(i)
        page['/Resources']['/XObject']['/Fm0'].clear()
        page['/Resources']['/XObject']['/Fm1'].clear()
        page['/Resources']['/XObject']['/Fm2'].clear()
        page['/Resources']['/XObject']['/Fm3'].clear()
        fm4 = page.get('/Resources').get('/XObject').get('/Fm4')
        if fm4:
            page['/Resources']['/XObject']['/Fm4'].clear()
        output.addPage(page)
    with open("out.pdf", 'wb') as ouf:
        output.write(ouf)

# page['/Resources']即为页面结构，分析结构，找出其中需要删除的元素即可。
